package party.loveit.download.core;


import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import java.io.File;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import party.loveit.download.listener.IDListener;


/**
 * Created by MRKING on 2017/1/4.
 */

public class DLService extends Service {

    private static final ConcurrentHashMap<String, Integer> TASK_NOTIFACTION_NUM  = new ConcurrentHashMap<>();
    private static final ConcurrentHashMap<String, NotificationCompat.Builder> BUILDERS = new ConcurrentHashMap<>();
    private static final ConcurrentHashMap<String, Integer> FILE_LENGTH = new ConcurrentHashMap<>();
    private static int index = 0;
    private NotificationManager nm;

    @Override
    public void onCreate() {
        nm =  (NotificationManager) getSystemService(Context
                .NOTIFICATION_SERVICE);
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String url = intent.getStringExtra("url");
        String imgUrl = intent.getStringExtra("imgUrl");
        String videoName = intent.getStringExtra("videoName");
        String videoTime = intent.getStringExtra("videoTime");


        DLManager.getInstance().dlStart(url, imgUrl, videoName, videoTime, null, new IDListener() {
            @Override
            public void onPrepare(String baseUrl) {

            }

            @Override
            public void onStart(String fileName, String baseUrl, long fileLength) {
                final NotificationCompat.Builder builder = new NotificationCompat.Builder(DLService.this)
                        .setSmallIcon(DLManager.getInstance().getNotificationRes());
                builder.setContentTitle(fileName);
                BUILDERS.put(baseUrl, builder);
                index ++;
                TASK_NOTIFACTION_NUM.put(baseUrl, index);
                FILE_LENGTH.put(baseUrl,(int)fileLength);
                DLListenerMgr.getInstance().onStart(fileName, baseUrl, fileLength);

            }

            @Override
            public void onProgress(String baseUrl, String speed, long progress) {
                DLListenerMgr.getInstance().onProgress(baseUrl, speed, progress);
                if(BUILDERS.containsKey(baseUrl)){
                    NotificationCompat.Builder builder = BUILDERS.get(baseUrl);
                    builder.setProgress(FILE_LENGTH.get(baseUrl), (int) progress, false);
                    nm.notify(TASK_NOTIFACTION_NUM.get(baseUrl), builder.build());
                }
            }

            @Override
            public void onStop(String baseUrl, long progress) {
                DLListenerMgr.getInstance().onStop(baseUrl, progress);
                cancel(baseUrl);
            }

            @Override
            public void onFinish(String baseUrl, File file) {
                DLListenerMgr.getInstance().onFinish(baseUrl, file);
                cancel(baseUrl);

            }

            @Override
            public void onError(String baseUrl, int status, String error) {
                DLListenerMgr.getInstance().onError(baseUrl, status, error);
                cancel(baseUrl);
            }
        });

        return super.onStartCommand(intent, flags, startId);
    }

    private void cancel(String baseUrl) {
        if(TASK_NOTIFACTION_NUM.containsKey(baseUrl)) {
            nm.cancel(TASK_NOTIFACTION_NUM.get(baseUrl));
            TASK_NOTIFACTION_NUM.remove(baseUrl);
            BUILDERS.remove(baseUrl);
            FILE_LENGTH.remove(baseUrl);
        }
    }

    @Override
    public void onDestroy() {
        closeAllNoti();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void closeAllNoti(){
        Set<String> keys = TASK_NOTIFACTION_NUM.keySet();
        if(keys != null){
            for (String key:keys){
                nm.cancel(TASK_NOTIFACTION_NUM.get(key));
            }
        }
    }
}
